﻿#region Usings

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;

#endregion

namespace Rail.Net.Core.Model.Nonpersistent
{
    /// <summary>
    ///   Represents a train model within a connection/route.
    ///   It contains information like: Train label, Train number, Departure station, Arrival station and Steps stations
    /// </summary>
    [Serializable]
    [DataContract]
    public class TrainModel : IEquatable<TrainModel>
    {
        #region Fields

        private SortedSet<StationModel> _stationSet; 

        #endregion

        #region Constructors

        public TrainModel()
        {
            Steps = new List<StationModel>();
        }

        #endregion

        #region Properties

        /// <summary>
        ///   Id
        /// </summary>
        [DataMember]
        public int Id { get; set; }

        /// <summary>
        ///   Train label
        /// </summary>
        [DataMember]
        public string Label { get; set; }

        /// <summary>
        ///   Train number
        /// </summary>
        [DataMember]
        public string Number { get; set; }

        /// <summary>
        ///   Journey type: train, walk or transfer
        /// </summary>
        [DataMember]
        public string JourneyType { get; set; }

        /// <summary>
        ///   Departure station
        /// </summary>
        [DataMember]
        public StationModel Departure { get; set; }

        /// <summary>
        ///   Arrival station
        /// </summary>
        [DataMember]
        public StationModel Arrival { get; set; }

        /// <summary>
        ///   Steps stations
        /// </summary>
        [DataMember]
        public List<StationModel> Steps { get; set; }

        public List<string> Rus
        {
            get { return Steps.Select(s => s.Ru).Distinct().ToList(); }
        }

        /// <summary>
        ///   Route description of all train stations
        /// </summary>
        public string Route
        {
            get
            {
                var result = new StringBuilder();
                if (Steps != null)
                {
                    Steps.ForEach(s => result.Append(s.Eva));
                }
                return result.ToString();
            }
        }

        /// <summary>
        /// Station set of steps
        /// </summary>
        public SortedSet<StationModel> StationSet
        {
            get {
                return _stationSet ??
                       (_stationSet = Steps != null ? new SortedSet<StationModel>(Steps) : new SortedSet<StationModel>());
            }
        } 

        #endregion

        #region Methods

        public bool Equals(TrainModel other)
        {
            return Id == other.Id && Label == other.Label && Number == other.Number;
        }

        #endregion
    }
}